<?php
class vendor_draft {

	var $CI;
	var $vendor_draft_model;

	public function __construct() {
		$this->CI =& get_instance();
		$this->CI->load->model("vendor/ven_vendor_draft_model");
		$this->vendor_draft_model =$this->CI->ven_vendor_draft_model;
	}

	public function get_vendor_drafts($parent_id) {
		return $this->vendor_draft_model->query_vendor_drafts($parent_id);
	}
	public function get_parent_id($vendor_draft_id){
		$vendor_draft= $this->vendor_draft_model->query_parent_id($vendor_draft_id);
		if ($vendor_draft) {
			return $vendor_draft['parent_id'];
		}else{
			return -1;
		}
	}

	public function save_vendor_draft($vendor_draft){
		$check_result=$this->check_vendor_draft($vendor_draft);
		if (!$check_result['result']) {
			return $check_result;
		}
		$vendor_draft['create_time'] 	=date("Y-m-d H:i:s");
		$vendor_draft['create_by'] 		=$_SESSION['user_name'];
		$vendor_draft['modify_time'] 	=date("Y-m-d H:i:s");
		$vendor_draft['modify_by'] 		=$_SESSION['user_name'];
		$vendor_draft['vendor_draft_status']=0;
		if (!$vendor_draft['unique_id']) {
			$vendor_draft['unique_id']=uniqid(md5(rand()), true);
		}
		$vendor_draft_id =$this->vendor_draft_model->insert_vendor_draft($vendor_draft);
		
		
		$org_vendor_draft ="";
		if ($vendor_draft['quote']=="") {
			$action_type="create";
		}else{
			$this->CI->load->library("ven/vendor");
			$org_vendor_draft =	$this->CI->vendor->get_vendor_info($vendor_draft['quote']);
			$action_type="re_create";
		}

		
		$this->CI->load->library("ven/vendor_draft_log");
		
		$this->CI->vendor_draft_log->insert_log($vendor_draft_id,$action_type,$vendor_draft['create_by'],$vendor_draft['create_time'],$vendor_draft['explanation'],$org_vendor_draft,$vendor_draft);
		$result['result']=true;
		$result['redirect_url']=base_url()."index.php/ven_vendor_manage";
		return $result;
	}

	public function check_vendor_draft($vendor_draft,$vendor_draft_id=""){
		$result['result'] =true;
		$result['message']="供应商资料填写正确";
		$check_name_result =$this->check_vendor_draft_name($vendor_draft['company_name'],$vendor_draft['quote']);
		if (!$check_name_result['result']) {
			return $check_name_result;
		}
		if ($vendor_draft['province']==""||$vendor_draft['province']=="0") {
			$result['result'] =false;
			$result['message']="请选择供应商所在的区域";
			return $result;
		}
		if ($vendor_draft['city']=="") {
			$result['result'] =false;
			$result['message']=$vendor_draft['city'];
			return $result;
		}if ($vendor_draft['region']=="") {
			$result['result'] =false;
			$result['message']="请选择供应商所在的地区";
			return $result;
		}
		if ($vendor_draft['address']=="") {
			$result['result'] =false;
			$result['message']="公司实际地址不能为空";
			return $result;
		}
		if ($vendor_draft['contactsname0']=="") {
			$result['result'] =false;
			$result['message']="联系人1的姓名不能为空";
			return $result;
		}

		if ($vendor_draft['contactspost0']=="") {
			$result['result'] =false;
			$result['message']="联系人1的职务不能为空";
			return $result;
		}
		if ($vendor_draft['contactsphone0']=="") {
			$result['result'] =false;
			$result['message']="联系人1的联系电话不能为空";
			return $result;
		}
		return $result;
	}


	public function check_vendor_draft_name($vendor_draft_name,$quote=""){
		$result['result'] =true;
		$result['message']="供应商名称可以使用";
		if ($vendor_draft_name=="") {
			$result['result']=false;
			$result['message']="供应商名称不能为空";
			return $result;
		}
		$this->CI->load->model("vendor/ven_vendor_model");
		$vendor_info =$this->CI->ven_vendor_model->query_vendor_by_name($vendor_draft_name);
		if ($vendor_info==null) {
			return $result;
		}
		if ($quote!=$vendor_info['vendor_id']) {
			$result['result']=false;
			$result['message']="供应商名称已经存在,请重新输入";
			return $result;
		}
		return $result;
	}
	public function get_vendor_draft_info($vendor_draft_id){
		return $this->vendor_draft_model->query_vendor_draft_info($vendor_draft_id);
	}

	public function get_vendor_draft_info_by_name($vendor_draft_name){
		return $this->vendor_draft_model->query_vendor_draft_info_by_name($vendor_draft_name);
	}

	public function delete_vendor_draft($vendor_draft_id){
		$this->vendor_draft_model->delete_vendor_draft($vendor_draft_id);

	}
	public function update_vendor_draft($vendor_draft,$vendor_draft_id){
		$check_result=$this->check_vendor_draft($vendor_draft,$vendor_draft_id);
		if (!$check_result['result']) {
			return $check_result;
		}
		$org_vendor_draft =$this->get_vendor_draft_info($vendor_draft_id);
		$vendor_draft['modify_time'] 	= date("Y-m-d H:i:s");
		$vendor_draft['modify_by'] 		= $_SESSION['user_name'];
		$vendor_draft['vendor_draft_status']=0;
		$affected_rows =$this->vendor_draft_model->update_vendor_draft($vendor_draft,$vendor_draft_id);

		if($affected_rows){
			$this->CI->load->library("ven/vendor_draft_log");
			$this->CI->vendor_draft_log->insert_log($vendor_draft_id,'update',$_SESSION['user_name'],date("Y-m-d H:i:s"),$vendor_draft['explanation'],$org_vendor_draft,$vendor_draft);
		}

		$result['result']=true;
		$result['redirect_url']=base_url()."index.php/ven_vendor_manage";
		return $result;
	}

	public function submit_check_draft($vendor_draft_id){
		$affected_rows =$this->vendor_draft_model->submit_check_draft($vendor_draft_id);
		if($affected_rows){
			$this->CI->load->library("ven/vendor_draft_log");
			$this->CI->vendor_draft_log->insert_log($vendor_draft_id,'submit_check',$_SESSION['user_name'],date("Y-m-d H:i:s"),'');
		}
	}

	public function query_my_vendor_draft_list($user_name,$user_id){
		$start		=	intval($this->CI->input->get("per_page"));
		$limit		=	$this->CI->config->item("page_size");
		return	$this->vendor_draft_model->query_my_vendor_draft_list($user_name,$user_id,$start,$limit);
	}
	public function query_my_vendor_draft_num($user_name,$user_id){
		return $this->vendor_draft_model->query_my_vendor_draft_num($user_name,$user_id);
	}

	public function query_check_vendor_draft_list($user_id){
		$start		=	intval($this->CI->input->get("per_page"));
		$limit		=	$this->CI->config->item("page_size");
		return	$this->vendor_draft_model->query_check_vendor_draft_list($user_id,$start,$limit);
	}
	public function query_check_vendor_draft_num($user_id){
		return $this->vendor_draft_model->query_check_vendor_draft_num($user_id);
	}

	public function allow_draft($vendor_draft_id){
		$vendor_draft =$this->get_vendor_draft_info($vendor_draft_id);
		if ($vendor_draft['vendor_draft_status']==10) {

			$vendor['company_name'] =$vendor_draft['company_name'];
			$vendor['province'] =$vendor_draft['province'];
			$vendor['city'] =$vendor_draft['city'];
			$vendor['region'] =$vendor_draft['region'];
			$vendor['registered_capital'] =$vendor_draft['registered_capital'];
			$vendor['registration_time'] =$vendor_draft['registration_time'];
			$vendor['corpscale'] =$vendor_draft['corpscale'];
			$vendor['employees'] =$vendor_draft['employees'];
			$vendor['license_address'] =$vendor_draft['license_address'];
			$vendor['address'] =$vendor_draft['address'];
			$vendor['legal_person'] =$vendor_draft['legal_person'];
			$vendor['boss'] =$vendor_draft['boss'];
			$vendor['phone'] =$vendor_draft['phone'];
			$vendor['fax'] =$vendor_draft['fax'];
			$vendor['highway_media'] =$vendor_draft['highway_media'];
			$vendor['airport_media'] =$vendor_draft['airport_media'];
			$vendor['led_media'] =$vendor_draft['led_media'];
			$vendor['cityad_media'] =$vendor_draft['cityad_media'];
			$vendor['highway_count'] =$vendor_draft['highway_count'];
			$vendor['airport_count'] =$vendor_draft['airport_count'];
			$vendor['led_count'] =$vendor_draft['led_count'];
			$vendor['cityad_count'] =$vendor_draft['cityad_count'];
			$vendor['popularity'] =$vendor_draft['popularity'];
			$vendor['credit'] =$vendor_draft['credit'];
			$vendor['price_type'] =$vendor_draft['price_type'];
			$vendor['decision_type'] =$vendor_draft['decision_type'];
			$vendor['license_valid'] =$vendor_draft['license_valid'];
			$vendor['orgcode_valid'] =$vendor_draft['orgcode_valid'];
			$vendor['tax_valid'] =$vendor_draft['tax_valid'];
			$vendor['background'] =$vendor_draft['background'];

			$vendor['contactsname0'] =$vendor_draft['contactsname0'];
			$vendor['contactspost0'] =$vendor_draft['contactspost0'];
			$vendor['contactsphone0'] =$vendor_draft['contactsphone0'];
			$vendor['contactsname1'] =$vendor_draft['contactsname1'];
			$vendor['contactspost1'] =$vendor_draft['contactspost1'];
			$vendor['contactsphone1'] =$vendor_draft['contactsphone1'];
			$vendor['contactsname2'] =$vendor_draft['contactsname2'];
			$vendor['contactspost2'] =$vendor_draft['contactspost2'];
			$vendor['contactsphone2'] =$vendor_draft['contactsphone2'];
				
			$vendor['unique_id'] =$vendor_draft['unique_id'];

			$vendor['modify_time'] 	=date("Y-m-d H:i:s");
			$vendor['modify_by'] 	=$_SESSION['user_name'];

			$this->CI->load->library("ven/vendor");
			if ($vendor_draft['quote']=="0") {
				$vendor['create_time'] 	=date("Y-m-d H:i:s");
				$vendor['create_by'] 	=$_SESSION['user_name'];

				$this->CI->vendor->insert_vendor($vendor);
			}else {
				$this->CI->vendor->update_vendor($vendor,$vendor_draft['quote']);
			}
			$affected_rows=$this->vendor_draft_model->set_delete_vendor_draft($vendor_draft_id);

			if($affected_rows){
				$this->CI->load->library("ven/vendor_draft_log");
				$this->CI->vendor_draft_log->insert_log($vendor_draft_id,'allow',$_SESSION['user_name'],date("Y-m-d H:i:s"),'');
			}

		}
	}

	public function not_allow_draft($vendor_draft_id,$explanation){
		$affected_rows =$this->vendor_draft_model->not_allow_draft($vendor_draft_id);
		if($affected_rows){
			$this->CI->load->library("ven/vendor_draft_log");
			$this->CI->vendor_draft_log->insert_log($vendor_draft_id,'not_allow',$_SESSION['user_name'],date("Y-m-d H:i:s"),$explanation);
		}
	}


	public function search_my_vendor_draft_list($user_name,$user_id,$search){
		$start		=	intval($this->CI->input->get("per_page"));
		$limit		=	$this->CI->config->item("page_size");
		return	$this->vendor_draft_model->search_my_vendor_draft_list($user_name,$user_id,$start,$limit,$search);
	}
	public function search_my_vendor_draft_num($user_name,$user_id,$search){
		return $this->vendor_draft_model->search_my_vendor_draft_num($user_name,$user_id,$search);
	}

	public function search_check_vendor_draft_list($user_id,$search){
		$start		=	intval($this->CI->input->get("per_page"));
		$limit		=	$this->CI->config->item("page_size");
		return	$this->vendor_draft_model->search_check_vendor_draft_list($user_id,$start,$limit,$search);
	}
	public function search_check_vendor_draft_num($user_id,$search){
		return $this->vendor_draft_model->search_check_vendor_draft_num($user_id,$search);
	}



}
?>